package es.antonio.duarte.abrahamAdmin.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.security.context.SecurityContextHolder;

/**
 * Invalida la sesion y devuelve a la página de login.
 */
public class SalirAction extends BaseAction {

   /** Manejador de trazas. */
   private final Log log = LogFactory.getLog(SalirAction.class);

   /**
    * Ejecucion del action. Invalida la sesion.
    * @param mapping ActionMapping asociado al action
    * @param form ActionForm asocido al action
    * @param request HttpServletRequest asociado al action
    * @param response HttpServletResponse asociado al action
    * @return ActionForward
    * @throws Exception En caso de error en la ejecucion del action
    * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
    *      org.apache.struts.action.ActionForm,
    *      javax.servlet.http.HttpServletRequest,
    *      javax.servlet.http.HttpServletResponse)
    */
   @Override
   public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      logDebug(log, "Invalidando la sesion");
      request.getSession().invalidate();
      logDebug(log, "Limpiando contexto de seguridad de spring");
      SecurityContextHolder.clearContext();
      logDebug(log,
               "Sesion invalidada, devolviendo forward 'salir'");
      return mapping.findForward("salir");
   }

}
